package com.java.controller.admin;

import java.text.DateFormat;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;


import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.JSONObject;
import com.java.entity.Money;
import com.java.entity.Order;
import com.java.entity.Payment;
import com.java.entity.User;
import com.java.service.impl.MoneyServiceImpl;
import com.java.service.impl.UserServiceImpl;




@Controller
@RequestMapping("/money/")
public class MoneyController {

	@Autowired
	private UserServiceImpl us;

	@Autowired
	private MoneyServiceImpl ms;

	@ResponseBody // 用来返回特定的值
	@RequestMapping("user")
	@Transactional
	public ModelAndView userSendDposit(String username, Float money) {
		ModelAndView mav = new ModelAndView();
		mav.setViewName("/pages/payment/recharge.jsp");// 付款界面

		User u = new User();
		u = us.selectByName(username);

		if (u == null) {
			mav.getModel().put("status", "不存在这个人");
		} else {
			Money m = new Money();
			m.setAmout(money);
			m.setUsername(username);
			ms.insert(m);
			mav.getModel().put("status", "审核中，请耐心等待");
			System.out.println(m);
		}
		return mav;
	}
	
	
	
	
	@ResponseBody // 用来返回特定的值
	@RequestMapping("check_money")
	@Transactional
	public ModelAndView passMav() {
		ModelAndView mav = new ModelAndView();
		mav.setViewName("/pages/return/checkmoney.jsp");// 付款界面
		List<Money> m = ms.all();
		mav.getModel().put("list", m);
		return mav;
	}
	
	
	

	@ResponseBody // 用来返回特定的值
	@RequestMapping("all")
	public String userSendDposit(){
		
		List<Money> m = ms.all();

		JSONArray array = new JSONArray();

		List<Map<String, Object>> alist = new ArrayList<Map<String, Object>>();
		for (int i = 0; i < m.size(); i++) {
			JSONObject jsonObj = new JSONObject();
			HashMap<String, Object> map = new HashMap<String, Object>();
			
			map.put("id", m.get(i).getId());
			
			DateFormat df1 = DateFormat.getDateInstance();
			System.out.println(df1.format(m.get(i).getDate()));
			
			
			map.put("time", df1.format(m.get(i).getDate()));
			map.put("amount", m.get(i).getAmout());
			map.put("pass", m.get(i).getPass());
			map.put("toPass", "/money/pass.shtml?id="+m.get(i).getId());
		
			array.add(map);
		}
		return array.toJSONString();
	}

	@ResponseBody // 用来返回特定的值
	@RequestMapping("pass")
	@Transactional
	public Integer pass(Integer id) {

		Money checkMoney = new Money();
		User u = new User();
		checkMoney = ms.selectById(id);
		if (checkMoney.getPass().equals(true)) {
			u = us.selectByName(checkMoney.getUsername());
			System.out.println(u.getDeposit());
			
			return -1;

		} else {

			Money m = new Money();
			m = ms.selectById(id);			
			m.setPass(true);
			
			u = us.selectByName(checkMoney.getUsername());
			float oAmont = u.getMoney();
			
			
		
			u.setMoney(m.getAmout()+oAmont);	 
			us.updateByPrimaryKeySelective(u); 
				
			return ms.update(m);
		}	
	}

}
